'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 士兵
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var _ = require('lodash');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var soldierSchema = require('../schemas/soldierSchema');
var Soldier = mongoConnector().model('Soldier');
var dataApi = require('../libs/dataApi');
var buildType = require('../consts/buildType');
var buildModel = require('../models/buildModel');
var roleModel = require('../models/roleModel');
var soldierConf = require('../config/soldierConf');
var soldierService = require('../services/soldierService.js');
var PlayerManager = require('../libs/playerManager');

/**
 * 根据角色id获取士兵数据
 * @param  {[type]}   role_id [description]
 * @param  {Function} cb      [description]
 * @return {[type]}           [description]
 */
module.exports.getByRoleId = function (role_id, cb) {
    function afterCreate(err, model) {
        cb(null, model);
    }

    function afterFindOne(err, schema) {
        if (!!err) {
            return cb(err);
        }
        if (!!schema) {
            cb(null, schema);
        } else {
            Soldier.create({
                role_id: role_id
            }, afterCreate);
        }
    }

    Soldier.findOne({
        role_id: role_id
    }, afterFindOne);
};

/**
 * 按条件参数查找
 */
module.exports.findWhere = function (where, cb) {
    Soldier.find(where, cb);
};

/**
 * 5点刷新
 */
module.exports.onFiveRefresh = function (self, cb) {
    // 重置神庙相关信息
    self.refresh();
    cb();
};

/**
 * 按战力查询
 */
module.exports.findByPower = function (role_id, soldier_power, count, cb) {
    Soldier.find({
            select_soldier_power: {
                $gt: soldier_power
            },
            role_id: {
                $ne: role_id
            }
        })
        .sort({
            select_soldier_power: 1
        })
        .limit(count)
        .exec(function (err, list) {
            if (!!err || !list) {
                return cb(err);
            }
            if (list.length < count) {
                _findByLessPower(role_id, soldier_power, count - list.length, function (err, less_list) {
                    if (!!err || !less_list) {
                        return cb(err);
                    }
                    _.forEach(less_list, function (item) {
                        list.push(item);
                    });
                    list = _.orderBy(list, ['select_soldier_power']);
                    cb(err, list);
                });
            } else {
                list = _.orderBy(list, ['select_soldier_power']);
                cb(err, list);
            }
        });
};

var _findByLessPower = function (role_id, soldier_power, count, cb) {
    Soldier.find({
            select_soldier_power: {
                $lte: soldier_power
            },
            role_id: {
                $ne: role_id
            }
        })
        .sort({
            select_soldier_power: -1
        })
        .limit(count)
        .exec(cb);
};
